<template>
	<view class="page index">
		<view class="form">
			<common-input
			  title="签约项目" 
				placeholder="请选择" 
				v-model="detail.projectName"
				:border-bottom="false"
				:disabled="true"
			>
			</common-input>
			<common-input
			  title="合同名称" 
				placeholder="请填写合同名称" 
				v-model="detail.name"
				:border-bottom="false"
			>
			</common-input>
			<common-input
			  title="合同编号" 
				placeholder="请填写合同编号" 
				v-model="detail.no"
				:border-bottom="false"
			>
			</common-input>
			<common-input
			  title="合同金额" 
				placeholder="请填写合同金额(人民币/元)" 
				v-model="detail.totalPrice"
				:border-bottom="false"
			>
			</common-input>
			<picker mode="date" @change="selectStartTime">
				<view class="picker">
					<common-input
					  title="合同开始日期" 
						placeholder="请填写合同开始日期" 
						v-model="detail.startTime"
						:border-bottom="false"
					>
					</common-input>
				</view>
			</picker>
			<picker mode="date" @change="selectEndTime">
				<view class="picker">
					<common-input
					  title="合同结束日期" 
						placeholder="请填写合同结束日期" 
						v-model="detail.endTime"
						:border-bottom="false"
					>
					</common-input>
				</view>
			</picker>
			<picker mode="date" @change="selectSignTime">
				<view class="picker">
					<common-input
					  title="合同签订日期" 
						placeholder="请填写合同签订日期" 
						v-model="detail.orderDate"
						:border-bottom="false"
					>
					</common-input>
				</view>
			</picker>
			<picker mode="selector" :range="signContactList" @change="selectContact">
				<view class="picker">
					<common-input
					  title="客户方签约人" 
						placeholder="请选择" 
						v-model="detail.signContactName"
						:disabled="true"
						:require="false"
						:border-bottom="false"
					>
					</common-input>
				</view>
			</picker>
			<picker mode="selector" :range="signUserList" @change="selectUser">
				<view class="picker">
					<common-input
					  title="公司方签约人" 
						placeholder="请选择" 
						v-model="detail.signUserName"
						:disabled="true"
						:require="false"
						:border-bottom="false"
					>
					</common-input>
				</view>
			</picker>
			<common-input
			  title="合同存放位置" 
				placeholder="请输入合同存放位置" 
				v-model="detail.savePosition"
				:require="false"
			>
			</common-input>
			<view class="text">
				<view class="title">
					<view>
						备注
					</view>
					<!-- <view class="require"></view> -->
				</view>
				<textarea v-model="detail.remark" placeholder="请输入备注消息" placeholder-class="place"></textarea>
			</view>
			<update-file :require="true" :paths="paths" title="合同文件" @update="updatefiles"></update-file>
		</view>
		<view class="footer">
			<!-- <button class="cancel">保存为草稿</button> -->
			<button class="confire" @click="save">提交审批</button>
		</view>
	</view>
</template>

<script>
	import commonInput from "@/components/commonInput.vue";
	import updateFile from "@/components/updateFile.vue";
	import {getProjectDetailApi} from "@/api/project_api.js";
	import {getUserListApi} from "@/api/common.js";
	import {updateContractApi} from "@/api/agreement_api.js";
	export default{
		data() {
			return{
				signContactIndex: -1,
				signContactList: ['还未选择签约项目'],
				signContactId: [],//客户签约人
				signUserIndex: -1,
				signUserList: ['未找到用户'],
				signUserId:[],//公司签约人
				fileUrls: [],
				paths: [],
				detail:{},//详情页传递参数
			}
		},
		components: {
			commonInput,
			updateFile
		},
		onLoad(e) {
			this.detail = JSON.parse(e.detail);
			this.detail.startTime = this.$share.getDate(this.detail.startTime);
			this.detail.endTime = this.$share.getDate(this.detail.endTime);
			this.detail.orderDate = this.$share.getDate(this.detail.orderDate);
			this.fileUrls = this.detail.fileUrls;
			for(let i = 0; i < this.detail.fileUrls.length; i++){
				this.paths.push({path:this.detail.fileUrls[i], name: i+1})
			}
			this.getContactList();
			this.getUserList();
		},
		methods:{
			// 获取项目联系人列表
			async getContactList(){
				let res = await getProjectDetailApi(this.detail.projectId).catch(() => {
					return Promise.reject();
				})
				this.signContactList = res.data.contacts.map(item => item.name);
				this.signContactId = res.data.contactIds;
			},
			// 选择客户方签约人
			selectContact(e){
				this.signContactIndex = e.detail.value;
				this.detail.signContactName = this.signContactList[this.signContactIndex];
				this.detail.signContactId = this.signContactId[this.signContactIndex];
			},
			// 获得公司方签约人列表
			async getUserList(){
				let res = await getUserListApi().catch(() => {
					return Promise.reject();
				})
				this.signUserList = res.data.map(item => item.nickname);
				this.signUserId = res.data.map(item => item.id);
			},
			// 选择公司方签约人
			selectUser(e){
				this.signUserIndex = e.detail.value;
				this.detail.signUserName = this.signUserList[this.signUserIndex];
				this.detail.signUserId = this.signUserId[this.signUserIndex];
			},
			selectStartTime(e){
				this.detail.startTime = e.detail.value;
			},
			selectEndTime(e){
				this.detail.endTime = e.detail.value;
			},
			selectSignTime(e){
				this.detail.orderDate = e.detail.value;
			},
			updatefiles(e){
				this.fileUrls = e.map(item => item.path)
			},
			save(){
				if(this.agreement == ''){
					uni.showToast({
						icon: 'none',
						title: '合同名称不能为空'
					})
					return;
				}
				if(this.code == ''){
					uni.showToast({
						icon: 'none',
						title: '合同编号不能为空'
					})
					return;
				}
				if(this.amount == ''){
					uni.showToast({
						icon: 'none',
						title: '合同金额不能为空'
					})
					return;
				}
				if(this.fileUrls.length == 0){
					uni.showToast({
						icon: 'none',
						title: '请上传文件'
					})
					return;
				}
				uni.showLoading({
					mask: true,
					title: '加载中...'
				})
				updateContractApi({
					id: this.detail.id,
					projectId: this.detail.projectId,
					customerId: this.detail.customerId,
					name: this.detail.name,
					no: this.detail.no,
					totalPrice: this.detail.totalPrice,
					startTime: new Date(this.detail.startTime).getTime(),
					endTime: new Date(this.detail.endTime).getTime(),
					orderDate: new Date(this.detail.orderDate).getTime(),
					signContactId: this.detail.signContactId,
					signUserId: this.detail.signUserId,
					remark:this.remark,
					fileUrls: this.fileUrls,
				}).then(() => {
					uni.hideLoading();
					uni.showToast({
						icon: 'success',
						title: '编辑成功'
					})
					setTimeout(() => {
						uni.navigateBack();
					},1500)
				}).catch(() => {
					uni.hideLoading();
				})
			},
		}
	}
</script>

<style lang="scss" scoped>
	@import "@/common/style/customicons.scss";
	.index{
		padding: 24rpx 0;
		@include flex-c;
		.form{
			flex: 1 0 0;
			overflow: auto;
			.text{
				border-bottom: 4rpx #e5e5e5 solid;
				padding: 12rpx 30rpx;
				.title{
					font-size: 28rpx;
					color: #101010;
					padding: 0 0 16rpx;
					display: flex;
					align-items: center;
					.require{
					  height: 28rpx;
					  width: 6rpx;
					  background-color: #c24135;
					  margin-left: 10rpx;
				  }
				}
				textarea{
					font-size: 28rpx;
					color: #909090;
					height: 125rpx;
				}
				.place{
					font-size: 28rpx;
					color: #909090;
				}
			}
			&::-webkit-scrollbar{
				display: none;
			}
		}
		.footer{
			flex: 0 0 auto;
			padding: 24rpx 30rpx;
			@include flex-r-center-start;
			button{
				@include button;
				color: #fff;
				height: 60rpx;
				width: 80%;
				line-height: 60rpx;
				font-size: 28rpx;
			}
			.cancel{
				background-color: #bebebe;
			}
			.confire{
				background-color: #198AFA;
			}
		}
	}
</style>